const database = require("../app/database");

class LabelServices {
  //获取所有标签
  async getalllbabel(start, size, search) {
    let str;
    if (start !== -1 && size !== -1) {
      str = `
        select * from label where content like '%${search}%' limit ?,?
      `;
      const [result] = await database.execute(str, [start, size]);
      const [re1] = await database.execute(
        `select * from label where content like '%${search}%'`
      );
      return {
        status: 200,
        result,
        total: re1.length,
      };
    } else {
      str = `
        select * from label;
      `;
      const [result] = await database.execute(str);
      return {
        status: 200,
        result,
      };
    }
  }
  //删除标签
  async deleteone(id) {
    const str = `
      delete from label where id=?
    `;
    await database.execute(str, [id]);
    return {
      status: 200,
      result: {
        message: "删除成功",
      },
    };
  }
  //添加标签
  async addlabel(body) {
    const { content } = body;
    const str = `
      insert into label (content) values(?)
    `;
    await database.execute(str, [content]);
    return {
      status: 200,
      result: {
        message: "添加成功",
      },
    };
  }
  //修改标签内容
  async editlabel(body) {
    const { content, id } = body;
    const str = `
      update label set content=? where id=? 
    `;
    await database.execute(str, [content, id]);
    return {
      status: 200,
      result: {
        message: "修改成功成功",
      },
    };
  }
}

module.exports = new LabelServices();
